<template>
  <view>
    <view class="box" @click="getRequest1"> get请求 request()</view>
    <view class="box" @click="getRequest2"> get请求 request().then</view>
    <view class="box" @click="getRequest3"> get请求 await,async方式</view>
    <view class="box" @click="postRequest1">post请求 参数</view>
  </view>
</template>

<script setup lang="js">
import { onLoad } from '@dcloudio/uni-app'
function getRequest1() {
  uni.request({
    url: 'https://jsonplaceholder.typicode.com/posts',
    // method: 'GET',//默认get请求
    success: (res) => {
      console.log(res)
    }
  })
}
function getRequest2() {
  uni.request({
    url: 'https://jsonplaceholder.typicode.com/posts',
    // method: 'GET',//默认get请求
  }).then(res=>{
    console.log(res)
  })
}
async function getRequest3()  {
  let res=await uni.request({
    url: 'https://jsonplaceholder.typicode.com/posts?id=1',
  })
  console.log(res)
}

 function postRequest1()  {
  uni.showLoading({title: '加载中....'})
  uni.request({
    url: 'https://jsonplaceholder.typicode.com/posts',
    method: 'POST',//默认get请求
    data:{//参数
      id:1
    },
    header:{//请求头
      'content-type':'application/json',//默认application/json
      'token':'xxxxx',
    },
    // 'dataType':true,//默认转成json
    timeout:2000,
    success:function(res){//请求成功
      console.log(res)
    },
    fail:function(err){//请求失败
      console.log(err)
    },
    complete:function(res){//成功失败都执行
      console.log(res)
      uni.hideLoading()
    }
  })

}
</script>


<style scoped lang="scss">
.box{
  height: 50px;
}
</style>
